<template>
  <div class="home-fullscreen">
    <el-header class="site-header">
      <div class="header-content">
        <div class="site-logo" @click="$router.push('/')">
          
          <span class="logo-text">个人博客系统</span>
        </div>
        <div class="header-actions">
          <template v-if="!isLoggedIn">
            <el-button class="auth-btn" type="primary" plain @click="$router.push('/login')">登录</el-button>
            <el-button class="auth-btn" type="success" @click="$router.push('/register')">注册</el-button>
          </template>
          <template v-else>
            <div class="user-info">
              <el-avatar :size="32" class="user-avatar">{{ usernameFirstChar }}</el-avatar>
              <span class="username">{{ username }}</span>
            </div>
            <el-button class="auth-btn" type="primary" plain @click="$router.push('/backend')">后台管理</el-button>
            <el-button class="auth-btn" type="danger" plain @click="handleLogout">退出</el-button>
          </template>
        </div>
      </div>
    </el-header>

    <div class="home-container">
      <div class="sidebar">
        <el-menu
          :default-active="activeMenu"
          class="category-menu"
          text-color="#333"
          active-text-color="#3273dc"
        >
          <el-menu-item index="all" @click="showAllArticles">
            <el-icon><Document /></el-icon>
            <span class="menu-label">全部文章</span>
          </el-menu-item>
          
          <el-sub-menu index="categories">
            <template #title>
              <el-icon><Files /></el-icon>
              <span class="menu-label">文章分类</span>
            </template>
            <el-menu-item 
              v-for="cat in categories" 
              :key="cat.id" 
              :index="'category-' + cat.id"
              @click="handleCategorySelect(cat.id.toString())"
            >
              <span class="menu-label">{{ cat.name }}</span>
            </el-menu-item>
          </el-sub-menu>
        </el-menu>
      </div>

      <div class="main-content">
        <div class="welcome-banner">
          <div class="banner-content">
            <h2 class="banner-title">精彩博客内容</h2>
            <p class="banner-text">在这里，你可以畅享阅读和分享知识的乐趣</p>
          </div>
          <div class="banner-image">
            <el-icon class="banner-icon" :size="60"><Reading /></el-icon>
          </div>
        </div>
      
        <div v-if="articles.length === 0" class="no-articles">
          <el-empty description="暂无文章" />
        </div>
        <div v-else>
          <div v-for="article in articles" :key="article.id" class="article-card" @click="viewArticle(article.id)">
            <div class="article-category-tag" v-if="article.categoryName">{{ article.categoryName }}</div>
            <h3 class="article-title">{{ article.title }}</h3>
            <div class="article-meta">
              <span class="meta-item"><el-icon><User /></el-icon> 作者：{{ article.nickname }}</span>
              <span class="meta-item"><el-icon><Clock /></el-icon> 发布时间：{{ formatDate(article.createTime) }}</span>
              <span class="meta-item"><el-icon><View /></el-icon> 浏览量：{{ article.viewCount }}</span>
            </div>
            <p class="article-summary">{{ article.content.substring(0, 200) }}...</p>
            <div class="article-footer">
              <div class="article-tags" v-if="article.tags && article.tags.length">
                <el-tag size="small" v-for="tag in article.tags" :key="tag" type="info" effect="plain" class="article-tag">{{ tag }}</el-tag>
              </div>
              <el-button size="small" text type="primary">阅读全文 <el-icon><ArrowRight /></el-icon></el-button>
            </div>
          </div>

          <div class="pagination-container">
            <el-pagination
              :current-page="currentPage"
              :page-size="pageSize"
              :total="total"
              :page-sizes="[5, 10, 20]"
              layout="total, sizes, prev, pager, next"
              background
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </div>
        </div>
        
        <footer class="site-footer">
          <div class="footer-content">
            <div class="footer-section">
              <h4 class="footer-title">关于我们</h4>
              <p class="footer-text">个人博客系统是一个分享知识和创意的平台，欢迎所有热爱写作的朋友加入我们。</p>
            </div>
          
            <div class="footer-section">
              <h4 class="footer-title">联系我们</h4>
              <ul class="footer-links">
                <li><el-icon><Message /></el-icon> 2919093668@qq.com</li>
                <li><el-icon><Phone /></el-icon> 18325029357</li>
              </ul>
            </div>
          </div>
          <div class="footer-bottom">
            <p>© {{ new Date().getFullYear() }} 个人博客系统 版权所有</p>
          </div>
        </footer>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { article, category } from '@/api'
import { User, Clock, View, ArrowRight, Document, Reading, Message, Phone, Files } from '@element-plus/icons-vue'

const router = useRouter()
const isLoggedIn = ref(!!localStorage.getItem('token'))
const username = ref(localStorage.getItem('username') || '')
const usernameFirstChar = computed(() => {
  return username.value ? username.value.charAt(0).toUpperCase() : 'U'
})
const categories = ref([])
const articles = ref([])
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)
const activeMenu = ref('all')
const activeCategoryId = ref('')

const loadCategories = async () => {
  try {
    const res = await category.list()
    categories.value = res
  } catch (error) {
    console.error(error)
  }
}

const loadArticles = async () => {
  try {
    const params = {
      current: currentPage.value,
      size: pageSize.value,
      status: 1  // 1表示已发布的文章
    }
    if (activeCategoryId.value) {
      params.categoryId = activeCategoryId.value
    }
    console.log('请求分页参数:', params);
    const res = await article.list(params)
    console.log('返回分页结果:', res);
    articles.value = res.records
    total.value = res.total
    
    // 添加虚拟标签用于展示
    articles.value = articles.value.map(article => {
      // 如果文章没有标签，添加一些示例标签
      if (!article.tags) {
        article.tags = ['博客', '分享', '知识']
      }
      return article
    })
  } catch (error) {
    console.error(error)
  }
}

const showAllArticles = () => {
  activeMenu.value = 'all'
  activeCategoryId.value = ''
  currentPage.value = 1
  loadArticles()
}

const handleCategorySelect = (categoryId) => {
  activeMenu.value = 'category-' + categoryId
  activeCategoryId.value = categoryId
  currentPage.value = 1
  loadArticles()
}

const handleSizeChange = (val) => {
  console.log('修改每页显示条数:', val);
  pageSize.value = val;
  currentPage.value = 1; // 切换每页条数时，重置为第一页
  loadArticles();
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  loadArticles()
}

const viewArticle = (id) => {
  router.push(`/article/${id}`)
}

const handleLogout = () => {
  localStorage.removeItem('token')
  localStorage.removeItem('username')
  isLoggedIn.value = false
  ElMessage.success('退出成功')
  router.push('/')
}

const formatDate = (date) => {
  return new Date(date).toLocaleString()
}

onMounted(() => {
  loadCategories()
  loadArticles()
})
</script>

<style scoped>
.home-fullscreen {
  width: 100%;
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background-color: #f5f7fa;
  background-image: 
    radial-gradient(circle at 25% 15%, rgba(50, 115, 220, 0.05) 2%, transparent 15%),
    radial-gradient(circle at 75% 85%, rgba(74, 138, 244, 0.05) 2%, transparent 15%),
    radial-gradient(circle at 85% 25%, rgba(100, 161, 255, 0.07) 2%, transparent 15%),
    radial-gradient(circle at 15% 75%, rgba(74, 138, 244, 0.05) 2%, transparent 15%);
  background-size: 60px 60px, 80px 80px, 70px 70px, 90px 90px;
  background-position: 0 0, 0 0, 0 0, 0 0;
}

.site-header {
  height: 60px;
  padding: 0 20px;
  background: linear-gradient(90deg, #3273dc, #4a8af4);
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.15);
  color: white;
  position: relative;
  z-index: 10;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 100%;
  max-width: 1400px;
  margin: 0 auto;
}

.site-logo {
  display: flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
}

.logo-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 8px;
  font-size: 20px;
}

.logo-text {
  font-size: 1.5rem;
  font-weight: 600;
  letter-spacing: 1px;
  background: linear-gradient(45deg, #ffffff, #e0e0e0);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

.header-actions {
  display: flex;
  gap: 10px;
}

.auth-btn {
  border-radius: 4px;
  font-weight: 500;
}

.home-container {
  display: flex;
  height: calc(100vh - 60px);
  overflow: hidden;
  max-width: 1400px;
  margin: 0 auto;
  width: 100%;
  position: relative;
  z-index: 5;
}

.sidebar {
  width: 240px;
  padding: 20px 15px;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(5px);
  border-right: 1px solid rgba(234, 234, 234, 0.7);
  height: 100%;
  overflow-y: auto;
  box-shadow: 2px 0 5px rgba(0, 0, 0, 0.03);
  border-radius: 0 0 0 10px;
}

.category-menu {
  border-right: none;
  background: transparent;
}

.menu-item {
  border-radius: 6px;
  margin-bottom: 5px;
}

.menu-label {
  font-size: 0.95rem;
}

.main-content {
  flex: 1;
  padding: 25px;
  overflow-y: auto;
  position: relative;
  background: rgba(255, 255, 255, 0.7);
  backdrop-filter: blur(5px);
  display: flex;
  flex-direction: column;
}

.main-content::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-image: url('data:image/svg+xml,%3Csvg width="60" height="60" viewBox="0 0 60 60" xmlns="http://www.w3.org/2000/svg"%3E%3Cg fill="none" fill-rule="evenodd"%3E%3Cg fill="%239C92AC" fill-opacity="0.03"%3E%3Cpath d="M36 34v-4h-2v4h-4v2h4v4h2v-4h4v-2h-4zm0-30V0h-2v4h-4v2h4v4h2V6h4V4h-4zM6 34v-4H4v4H0v2h4v4h2v-4h4v-2H6zM6 4V0H4v4H0v2h4v4h2V6h4V4H6z"/%3E%3C/g%3E%3C/g%3E%3C/svg%3E');
  z-index: -1;
  opacity: 0.5;
  border-radius: 0 0 10px 0;
}

.welcome-banner {
    background: linear-gradient(135deg, #3273dc, #6fa8dc);
    border-radius: 12px;
    padding: 20px 30px;
    margin-bottom: 25px;
    color: white;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
    display: flex;
    justify-content: space-between;
    align-items: center;
    overflow: visible;
    position: relative;
    width: 100%;
}

.banner-content {
  position: relative;
  z-index: 1;
  flex: 1;
  max-width: 80%;
}

.banner-title {
  font-size: 1.8rem;
  font-weight: 600;
  margin-bottom: 8px;
  white-space: nowrap;
  overflow: visible;
}

.banner-text {
  font-size: 1rem;
  opacity: 0.9;
}

.banner-image {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
}

.banner-icon {
  color: rgba(255, 255, 255, 0.2);
  font-size: 60px;
}

.welcome-banner::before {
  content: '';
  position: absolute;
  top: -50px;
  right: -50px;
  width: 200px;
  height: 200px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 50%;
}

.welcome-banner::after {
  content: '';
  position: absolute;
  bottom: -80px;
  left: -50px;
  width: 180px;
  height: 180px;
  background: rgba(255, 255, 255, 0.08);
  border-radius: 50%;
}

.no-articles {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 50%;
}

.article-card {
  background: white;
  border-radius: 8px;
  padding: 20px 25px;
  margin-bottom: 20px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.08);
  cursor: pointer;
  transition: all 0.3s ease;
  border-left: 4px solid #3273dc;
  position: relative;
  overflow: hidden;
}

.article-category-tag {
  position: absolute;
  top: 0;
  right: 0;
  background: linear-gradient(135deg, #3273dc, #4a8af4);
  color: white;
  font-size: 0.7rem;
  padding: 3px 12px;
  border-radius: 0 0 0 8px;
  font-weight: 500;
  letter-spacing: 0.5px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.article-card::after {
  content: '';
  position: absolute;
  top: 0;
  right: 0;
  width: 100px;
  height: 100px;
  background: linear-gradient(135deg, transparent 50%, rgba(50, 115, 220, 0.03) 50%);
  z-index: 0;
}

.article-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.12);
}

.article-title {
  font-size: 1.4rem;
  color: #333;
  margin: 0 0 12px;
  position: relative;
  z-index: 1;
  padding-right: 60px;
}

.article-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 15px;
  color: #666;
  margin-bottom: 15px;
  font-size: 0.9rem;
  position: relative;
  z-index: 1;
}

.meta-item {
  display: flex;
  align-items: center;
  gap: 5px;
}

.article-summary {
  color: #666;
  line-height: 1.6;
  margin-bottom: 15px;
  position: relative;
  z-index: 1;
}

.article-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-top: 1px solid #f0f0f0;
  padding-top: 12px;
  margin-top: 10px;
  position: relative;
  z-index: 1;
}

.article-tags {
  display: flex;
  gap: 5px;
  flex-wrap: wrap;
}

.article-tag {
  font-size: 0.7rem;
  padding: 0 8px;
  height: 22px;
  line-height: 22px;
}

.pagination-container {
  margin-top: 30px;
  margin-bottom: 40px;
  display: flex;
  justify-content: center;
}

.site-footer {
  margin-top: auto;
  padding-top: 30px;
  border-top: 1px solid rgba(0, 0, 0, 0.05);
  position: relative;
  z-index: 1;
}

.footer-content {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 30px;
}

.footer-section {
  flex: 1;
  min-width: 200px;
}

.footer-title {
  font-size: 1.1rem;
  color: #333;
  margin-bottom: 15px;
  position: relative;
  padding-bottom: 8px;
}

.footer-title::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: 40px;
  height: 2px;
  background: #3273dc;
}

.footer-text {
  color: #666;
  font-size: 0.9rem;
  line-height: 1.6;
}

.footer-links {
  list-style: none;
  padding: 0;
}

.footer-links li {
  margin-bottom: 8px;
  font-size: 0.9rem;
  display: flex;
  align-items: center;
  gap: 8px;
}

.footer-links a {
  color: #666;
  text-decoration: none;
  transition: color 0.3s;
}

.footer-links a:hover {
  color: #3273dc;
}

  .footer-bottom {
   margin-top: 30px;
   padding: 15px 0;
   text-align: center;
   font-size: 0.85rem;
   color: #666;
   border-top: 1px solid rgba(0, 0, 0, 0.05);
  }

.user-info {
  display: flex;
  align-items: center;
  margin-right: 10px;
}

.user-avatar {
  background: linear-gradient(45deg, #36d1dc, #5b86e5);
  color: white;
  font-weight: bold;
}

.username {
  margin-left: 8px;
  color: white;
  font-weight: 500;
}
</style>  